home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / fonts.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  330 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40. #include <GL/glx.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <X11/keysym.h>
  44. #include <string.h>
  45.  
  46. static int RGBattributes_DB[] = {
  47.     GLX_RGBA,
  48.     GLX_RED_SIZE, 1,
  49.     GLX_GREEN_SIZE, 1,
  50.     GLX_BLUE_SIZE, 1,
  51.     GLX_DOUBLEBUFFER,
  52.     None,
  53. };
  54.  
  55. static int CIattributes_DB[] = {
  56.     GLX_DOUBLEBUFFER,
  57.     None,
  58. };
  59.  
  60. enum {
  61.     BLACK = 0,
  62.     RED,
  63.     GREEN,
  64.     YELLOW,
  65.     BLUE,
  66.     MAGENTA,
  67.     CYAN,
  68.     WHITE
  69. };
  70.  
  71. #define COLOR_OFFSET_1    16
  72. #define COLOR_OFFSET_2    32
  73.  
  74.  
  75. static float rgbMap[][3] = {
  76.     {0, 0, 0},
  77.     {1, 0, 0},
  78.     {0, 1, 0},
  79.     {1, 1, 0},
  80.     {0, 0, 1},
  81.     {1, 0, 1},
  82.     {0, 1, 1},
  83.     {1, 1, 1}
  84. };
  85.  
  86. static long W = 300, H = 300;
  87. static Display *dpy;
  88. static Window window;
  89. static Colormap cmap;
  90. int doubleBuf = 1;
  91. int fontToggle = 0;
  92. GLenum drawBuffer = GL_FRONT;
  93. static long rgb;
  94. static long ci1 = BLUE, ci2 = GREEN;
  95.  
  96.  
  97. static LoadGLFont(GLuint *pBase, GLsizei *pCount)
  98. {
  99.     XFontStruct *fontInfo;
  100.     Font id;
  101.     int first, last;
  102.  
  103.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  104.     static char pattern2[] = "-adobe-helvetica-*-o-normal-*-18-*-*-*-*-*-*-*";
  105.     static char pattern3[] = "-sgi-screen-bold-*-normal-*-18-*-*-*-*-*-*-*";
  106.     static char pattern4[] = "-sgi-rock-*-*-*-*-18-*-*-*-*-*-*-*";
  107.  
  108.     fontToggle = fontToggle%4;
  109.     switch (fontToggle) {
  110.     case 0: fontInfo = XLoadQueryFont(dpy, pattern1);
  111.         break;
  112.     case 1: fontInfo = XLoadQueryFont(dpy, pattern2);
  113.         break;
  114.     case 2: fontInfo = XLoadQueryFont(dpy, pattern3);
  115.         break;
  116.     case 3: fontInfo = XLoadQueryFont(dpy, pattern4);
  117.         break;
  118.     }
  119.  
  120.     id = fontInfo->fid;
  121.     first = (int)fontInfo->min_char_or_byte2;
  122.     last = (int)fontInfo->max_char_or_byte2;
  123.     *pCount = last-first+1;
  124.  
  125.  
  126.     *pBase = glGenLists(last+1);
  127.     if (*pBase == 0) {
  128.         return 0;
  129.     }
  130.     glXUseXFont(id, first, *pCount, (int)(*pBase+first));
  131. }
  132.  
  133.  
  134. static void Usage(void)
  135. {
  136.     printf("Usage: font [-c]\n");
  137.     printf("   -c:  Run in color index mode\n");
  138.     printf("   f key rotates through fonts \n");
  139.     exit(-1);
  140. }
  141.  
  142. static void DoDisplay(void)
  143. {
  144.     GLfloat y = 5.0;
  145.     GLuint base;
  146.     GLsizei count;
  147.  
  148.     LoadGLFont(&base, &count);
  149.     glListBase(base);
  150.  
  151.     glLoadIdentity();
  152.     glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
  153.  
  154.     glDrawBuffer(GL_FRONT);
  155.     glClearColor(0.0, 0.0, 0.0, 0.0);
  156.     glClearIndex(0);
  157.     glClear(GL_COLOR_BUFFER_BIT);
  158.  
  159.     glRasterPos2f(10.0, y);
  160.     glCallLists(26, GL_UNSIGNED_BYTE, 
  161.     (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
  162.     y += 20.0;
  163.     glRasterPos2f(10.0, y);
  164.     glCallLists(26, GL_UNSIGNED_BYTE, 
  165.     (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
  166.     y += 20.0;
  167.     glRasterPos2f(10.0, y);
  168.     glCallLists(26, GL_UNSIGNED_BYTE, 
  169.     (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  170.     y += 20.0;
  171.     glRasterPos2f(10.0, y);
  172.     glCallLists(26, GL_UNSIGNED_BYTE, 
  173.     (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  174.     y += 20.0;
  175.     glRasterPos2f(10.0, y);
  176.     glCallLists(10, GL_UNSIGNED_BYTE, "0123456789"); 
  177.     y += 20.0;
  178.     glRasterPos2f(10.0, y);
  179.     glCallLists(10, GL_UNSIGNED_BYTE, "0123456789"); 
  180.  
  181.     y += 20.0;
  182.     glRasterPos2f(10.0, y);
  183.     glCallLists(25, GL_UNSIGNED_BYTE, 
  184.     (unsigned char *)"N O P Q R S T U V W X Y Z");
  185.     y += 20.0;
  186.     glRasterPos2f(10.0, y);
  187.     glCallLists(25, GL_UNSIGNED_BYTE, 
  188.     (unsigned char *)"! @ # $ % ^ & * ( ) _ + }");
  189.     glXWaitGL();
  190.  
  191.     glFlush();
  192.     glDeleteLists(base, count);
  193. }
  194.  
  195. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  196. {
  197.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  198.     return GL_TRUE;
  199.     }
  200.     return GL_FALSE;
  201. }
  202.  
  203.  
  204. int main(long argc, char** argv)
  205. {
  206.     XVisualInfo *vi;
  207.     XSetWindowAttributes swa;
  208.     GLXContext cx;
  209.     XEvent event;
  210.     GLboolean needDisplay;
  211.     int i;
  212.  
  213.     rgb = 1;
  214.     doubleBuf = 1;
  215.     for (i = 1; i < argc; i++) {
  216.         if (argv[i][0] == '-') {
  217.             switch (argv[i][1]) {
  218.               case 'c':
  219.                 rgb = 0;
  220.                 break;
  221.               default:
  222.                 Usage();
  223.             }
  224.         } else {
  225.             Usage();
  226.         }
  227.     }
  228.  
  229.     dpy = XOpenDisplay(0);
  230.     if (!dpy) {
  231.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  232.     return -1;
  233.     }
  234.  
  235.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  236.              (rgb ? RGBattributes_DB : CIattributes_DB));
  237.     if (!vi) {
  238.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  239.         getenv("DISPLAY"));
  240.     return -1;
  241.     }
  242.  
  243.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  244.                rgb ? AllocNone : AllocAll);
  245.     if (!rgb) {
  246.         XColor buf;
  247.         int i;
  248.  
  249.         buf.flags = DoRed | DoGreen | DoBlue;
  250.  
  251.         /* Init color map */
  252.         for (i=0; i<16; i++) {
  253.             buf.pixel = i;
  254.             buf.blue = (i & 4) ? 65535 : 0;
  255.             buf.green = (i & 2) ? 65535 : 0;
  256.             buf.red = (i & 1) ? 65535 : 0;
  257.             if (i > 8) {
  258.                 buf.red /= 2;
  259.                 buf.green /= 2;
  260.                 buf.blue /= 2;
  261.             }
  262.             XStoreColor(dpy, cmap, &buf);
  263.         }
  264.     }
  265.  
  266.  
  267.     swa.border_pixel = 0;
  268.     swa.colormap = cmap;
  269.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  270.     | KeyReleaseMask;
  271.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  272.                W, H,
  273.                0, vi->depth, InputOutput, vi->visual,
  274.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  275.     XSetWMColormapWindows(dpy, window, &window, 1);
  276.     XStoreName(dpy, window, "Fonts Test (optimized)");
  277.     XMapWindow(dpy, window);
  278.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  279.  
  280.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  281.     if (!glXMakeCurrent(dpy, window, cx)) {
  282.     fprintf(stderr, "Can't make window current to context\n");
  283.     return -1;
  284.     }
  285.  
  286.  
  287.  
  288.     needDisplay = GL_TRUE;
  289.     for (;;) {
  290.     do {
  291.         XNextEvent(dpy, &event);
  292.         switch (event.type) {
  293.           case Expose:
  294.         needDisplay = GL_TRUE;
  295.         break;
  296.           case ConfigureNotify:
  297.         W = event.xconfigure.width;
  298.         H = event.xconfigure.height;
  299.         needDisplay = GL_TRUE;
  300.         break;
  301.           case KeyPress:
  302.         {
  303.             char buf[100];
  304.             int rv;
  305.             KeySym ks;
  306.  
  307.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  308.             switch (ks) {
  309.               case XK_f:
  310.             fontToggle ++;
  311.             needDisplay = GL_TRUE;
  312.             break;
  313.               case XK_r:
  314.             needDisplay = GL_TRUE;
  315.             break;
  316.               case XK_Escape:
  317.             return 0;
  318.             }
  319.         }
  320.         break;
  321.         }
  322.     } while (XPending(dpy) != 0);
  323.  
  324.     if (needDisplay) {
  325.         needDisplay = GL_FALSE;
  326.         DoDisplay();
  327.     }
  328.     }
  329. }
  330.